package com.bilicute.libman.service.impl;

import com.bilicute.libman.mapper.BookMapper;
import com.bilicute.libman.pojo.Book;
import com.bilicute.libman.pojo.PageBean;
import com.bilicute.libman.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @所属包名: com.bilicute.libman.service.impl
 * @类名: BookServiceImpl
 * @作者: i囡漫笔
 * @描述: 图书Service交互实现类
 * @创建时间: 2024-06-21 14:23
 */

@Service
public class BookServiceImpl implements BookService {

    private BookMapper bookMapper;
    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public Book findBookById(Integer id) {
        return bookMapper.findBookById(id);
    }

    @Override
    public void borrow(Integer id, Integer loggedInUserId ,String recordId) {
        bookMapper.borrow(id,loggedInUserId,recordId);
    }

    @Override
    public void resultBook(Integer id) {
        bookMapper.resultBook(id);
    }

    @Override
    public void add(Book book) {
        book.setBook_status("0");
        bookMapper.add(book);
    }

    @Override
    public void off(Integer id) {
        bookMapper.off(id);
    }

    @Override
    public void pass(Integer id) {
        bookMapper.pass(id);
    }

    @Override
    public PageBean<Book> list(Integer pageNum, Integer pageSize, Integer book_status) {
        // 创建PageBean对象，用来封装查询好的数据
        PageBean<Book> bookPageBean = new PageBean<>();
// 开启分页查询(借助PageHelper)
        PageHelper.startPage(pageNum, pageSize);
// 调用Mapper完成查询
        List<Book> books = bookMapper.list(book_status);
// 使用PageInfo获取分页信息
        PageInfo<Book> pageInfo = new PageInfo<>(books);
// 把数据填充到PageBean对象中
        bookPageBean.setTotal(pageInfo.getTotal());
        bookPageBean.setItems(pageInfo.getList());
        return bookPageBean;
    }

    @Override
    public PageBean<Book> listMyBorrow(Integer pageNum, Integer pageSize, Integer book_status, Integer loggedInUserId) {
        // 创建PageBean对象，用来封装查询好的数据
        PageBean<Book> bookPageBean = new PageBean<>();
// 开启分页查询(借助PageHelper)
        PageHelper.startPage(pageNum, pageSize);
// 调用Mapper完成查询
        List<Book> books = bookMapper.listMyBorrow(book_status,loggedInUserId);
// 使用PageInfo获取分页信息
        PageInfo<Book> pageInfo = new PageInfo<>(books);
// 把数据填充到PageBean对象中
        bookPageBean.setTotal(pageInfo.getTotal());
        bookPageBean.setItems(pageInfo.getList());
        return bookPageBean;

    }

    @Override
    public void delete(Integer id) {
        bookMapper.delete(id);
    }
}
